#!/usr/bin/python
# -*- coding : UTF-8 -*-
# ========================
# @Time  : 2020/7/29 9:30
# @Author: Mecell
# @File  : training_param.py
# ========================
import random
import re
import json

from common.path_constants import TRAINING_PATTERN_CONF
from data.com_function import generate_random_no
from common.yaml_packaging import YamlHandle
from database.training_mysql import TrainingMysql
from common.mysql_packaging import MysqlHandle
from common.context_packaging import ContextHandle
from common.config_packaging import ConfigHandle
from parameterize.user_param import UserContextHandle as UserContext



class TrainingContextHandle(ContextHandle):
    """
    定义资产管理参数化，反射功能的类
    """
    training_pattern_conf = ConfigHandle(TRAINING_PATTERN_CONF)

    @classmethod
    def rep_com_course_info(cls, data):
        """
        替换公司课程信息
        :param data:
        :return:
        """
        course_name_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "course_name")))
        course_code_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "course_code")))
        course_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "course_id")))
        course_class_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "course_class_id")))
        last_course_class_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "last_course_class_id")))
        lecturer_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "lecturer_id")))
        last_lecturer_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "last_lecturer_id")))
        lecturer_name_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "lecturer_name")))
        video_flag_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "video_flag")))
        training_mysql = TrainingMysql()
        com_course_info = training_mysql.get_com_course_info()
        if re.search(course_name_pattern, data):
            rep_course_name = com_course_info.get('course_name')
            data = re.sub(course_name_pattern, rep_course_name, data)
        if re.search(course_code_pattern, data):
            rep_course_code = "C159" + generate_random_no(10)
            data = re.sub(course_code_pattern, str(rep_course_code), data)
        if re.search(course_id_pattern, data):
            rep_course_id = getattr(cls, 'course_id')
            data = re.sub(course_id_pattern, str(rep_course_id), data)
        if re.search(course_class_id_pattern, data):
            rep_course_class_id = com_course_info.get('classify_id')
            data = re.sub(course_class_id_pattern, str(rep_course_class_id), data)
        if re.search(last_course_class_id_pattern, data):
            rep_course_class_id = getattr(cls, 'course_classify_id')
            data = re.sub(last_course_class_id_pattern, str(rep_course_class_id), data)
        if re.search(lecturer_id_pattern, data):
            rep_lecturer_id = com_course_info.get('lecturer_id')
            data = re.sub(lecturer_id_pattern, str(rep_lecturer_id), data)
        if re.search(last_lecturer_id_pattern, data):
            rep_lecturer_id = getattr(cls, 'lecturer_id')
            data = re.sub(last_lecturer_id_pattern, str(rep_lecturer_id), data)
        if re.search(lecturer_name_pattern, data):
            rep_lecturer_name = com_course_info.get('lecturer_name')
            data = re.sub(lecturer_name_pattern, rep_lecturer_name, data)
        if re.search(video_flag_pattern, data):
            rep_video_flag = com_course_info.get('video_flag')
            data = re.sub(video_flag_pattern, str(rep_video_flag), data)
        training_mysql.close_sql()
        return data

    @classmethod
    def rep_case_info(cls, data):
        """
        替换案例中心信息
        :param data:
        :return:
        """
        case_type_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "case_type")))
        case_position_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "case_position_id")))
        case_class_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "case_class")))
        case_level_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "case_level")))
        training_mysql = TrainingMysql()
        case_info = training_mysql.get_case_info()
        if re.search(case_class_pattern, data):
            rep_case_type = case_info.get('type')
            data = re.sub(case_type_pattern, rep_case_type, data)
        if re.search(case_position_id_pattern, data):
            rep_case_position_id = case_info.get('position_id')
            data = re.sub(case_position_id_pattern, str(rep_case_position_id), data)
        if re.search(case_class_pattern, data):
            rep_case_class = case_info.get('category')
            data = re.sub(case_class_pattern, str(rep_case_class), data)
        if re.search(case_level_pattern, data):
            rep_case_level = case_info.get('level')
            data = re.sub(case_level_pattern, str(rep_case_level), data)
        training_mysql.close_sql()
        return data

    @classmethod
    def rep_last_uuid(cls, data):
        """
		替换上传文件返回的uuid
		:param data:
		:return:
		"""
        last_uuid_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "last_uuid")))
        if re.search(last_uuid_pattern, data):
            rep_uuid = getattr(cls, 'uuid')
            data = re.sub(last_uuid_pattern, rep_uuid, data)
        return data

    @classmethod
    def rep_last_case_id(cls, data):
        """
		替换案例的case_id和desc_id
		:param data:
		:return:
		"""
        case_id_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "case_id")))
        desc_id_1_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "desc_id_1")))
        desc_id_2_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "desc_id_2")))
        desc_id_3_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "desc_id_3")))
        desc_id_4_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "desc_id_4")))
        desc_id_5_pattern = re.compile(eval(cls.training_pattern_conf("com training_manage", "desc_id_5")))
        if re.search(case_id_pattern, data):
            rep_case_id = getattr(cls, 'case_id')
            data = re.sub(case_id_pattern, str(rep_case_id), data)
        if re.search(desc_id_1_pattern, data):
            rep_desc_id = getattr(cls, 'desc_id_1')
            data = re.sub(desc_id_1_pattern, str(rep_desc_id), data)
        if re.search(desc_id_2_pattern, data):
            rep_desc_id = getattr(cls, 'desc_id_2')
            data = re.sub(desc_id_2_pattern, str(rep_desc_id), data)
        if re.search(desc_id_3_pattern, data):
            rep_desc_id = getattr(cls, 'desc_id_3')
            data = re.sub(desc_id_3_pattern, str(rep_desc_id), data)
        if re.search(desc_id_4_pattern, data):
            rep_desc_id = getattr(cls, 'desc_id_4')
            data = re.sub(desc_id_4_pattern, str(rep_desc_id), data)
        if re.search(desc_id_5_pattern, data):
            rep_desc_id = getattr(cls, 'desc_id_5')
            data = re.sub(desc_id_5_pattern, str(rep_desc_id), data)
        return data

    @classmethod
    def rep_dep_course_info(cls, data):
        """
		替换部门课程信息
		:param data:
		:return:
		"""
        course_dept_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "course_dept")))
        course_name_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "course_name")))
        course_area_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "course_area")))
        course_create_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "course_create")))
        video_flag_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "video_flag")))
        platform_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "platform")))
        training_mysql = TrainingMysql()
        dep_course_info = training_mysql.get_dep_course_info()
        if re.search(course_name_pattern, data):
            rep_course_name = dep_course_info.get('course_name')
            data = re.sub(course_name_pattern, rep_course_name, data)
        if re.search(course_dept_pattern, data):
            rep_course_dept = dep_course_info.get('department_id')
            data = re.sub(course_dept_pattern, str(rep_course_dept), data)
        if re.search(course_area_pattern, data):
            rep_course_area = dep_course_info.get('office_area')
            data = re.sub(course_area_pattern, str(rep_course_area), data)
        if re.search(course_create_pattern, data):
            rep_course_class_id = dep_course_info.get('create_name')
            data = re.sub(course_create_pattern, str(rep_course_class_id), data)
        if re.search(video_flag_pattern, data):
            rep_video_flag = dep_course_info.get('video_flag')
            data = re.sub(video_flag_pattern, str(rep_video_flag), data)
        if re.search(platform_pattern, data):
            rep_platform = random.choice(dep_course_info.get('share_platform').split(','))
            data = re.sub(platform_pattern, str(rep_platform), data)
        training_mysql.close_sql()
        return data

    @classmethod
    def rep_dept_course_id(cls, data):
        """
        替换部门课程id
        :param data:
        :return:
        """
        course_id_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "course_id")))
        training_mysql = TrainingMysql()
        dep_course_info = training_mysql.get_dep_course_info()
        if re.search(course_id_pattern, data):
            rep_course_id = dep_course_info.get('course_id')
            data = re.sub(course_id_pattern, str(rep_course_id), data)
        training_mysql.close_sql()
        return data

    @classmethod
    def rep_dept_test_course_id(cls, data, test_flag):
        """
		替换部门试卷课程id
		:param data:
		:return:
		"""
        test_course_id_pattern = re.compile(eval(cls.training_pattern_conf("dep training_manage", "test_course_id")))
        training_mysql = TrainingMysql()
        dep_test_course_id = training_mysql.get_dep_test_course_id(test_flag)
        if re.search(test_course_id_pattern, data):
            rep_course_id = dep_test_course_id.get('course_id')
            data = re.sub(test_course_id_pattern, str(rep_course_id), data)
        training_mysql.close_sql()
        return data

    @classmethod
    def search_com_course_parameterize(cls, data):
        """
        搜索公司课程数据参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_com_course_info(data)
        return data

    @classmethod
    def sign_up_com_course_parameterize(cls, data):
        """
        报名公司课程数据参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_com_course_info(data)
        data = cls.rep_day_after(data, 0)
        return data

    @classmethod
    def search_case_parameterize(cls, data):
        """
        搜索案例数据参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_case_info(data)
        return data

    @classmethod
    def create_case_parameterize(cls, data):
        """
        新增案例数据参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_last_uuid(data)
        data = ContextHandle.rep_uuid(data)
        data = cls.rep_case_info(data)
        return data

    @classmethod
    def edit_case_parameterize(cls, data):
        """
		编辑案例数据参数化
		:param data:
		:return:
		"""
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_case_info(data)
        data = cls.rep_last_case_id(data)
        return data

    @classmethod
    def search_dep_course_parameterize(cls, data):
        """
		搜索部门课程数据参数化
		:param data:
		:return:
		"""
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_dep_course_info(data)
        return data

    @classmethod
    def view_dep_course_parameterize(cls, data):
        """
		查看部门课程详情数据参数化
		:param data:
		:return:
		"""
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_dept_course_id(data)
        return data

    @classmethod
    def dep_course_paper_parameterize(cls, data, test_flag):
        """
		新建,编辑部门试卷课程数据参数化
		:param data:
		:return:
		"""
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContext.rep_training_user_info(data)
        data = cls.rep_dept_test_course_id(data, test_flag)
        return data